home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / Utilities / CalibrateLuminance / GetALuminance.c < prev   
Encoding:
C/C++ Source or Header  |  1995-01-07  |  3.1 KB  |  99 lines  |  [TEXT/MMCC]

  1. /*
  2. GetALuminance.c
  3. © 1990-1995 Denis G. Pelli
  4. Two subroutines used by CalibrateLuminance.c. Probably not useful outside that context.
  5.  
  6. HISTORY:
  7. 7/28/90    dgp    wrote it.
  8. 9/24/90    dgp    changed nBackground to VBackground.
  9. 6/25/91 dgp fixed bug pointed out by Sujeet Paul whereby LToVHunt() made a superfluous
  10. call to GetALuminance() with frames=1, forcing use of the A/D even if it wasn't available.
  11. On a machine without an A/D card this would cause CalibrateLuminance to fail.
  12. On machines with an A/D card this wasted a second on each call to LToVHunt().
  13. 8/24/91    dgp    Made compatible with THINK C 5.0.
  14. 12/17/92 dgp Enhanced to support arbitrary dacSize. Replaced 256 by LP->VMax+1 
  15. in LToVHunt.
  16. 12/21/92 dgp No longer load unused dac bits.
  17. */
  18. #include "VideoToolbox.h"
  19. #include "Luminance.h"
  20.  
  21. double GetALuminance(LuminanceRecord *LP,GDHandle device,
  22.     int frames,double LuminancePerVoltage,int entry,int red,int green,int blue);
  23. void LToVHunt(LuminanceRecord *LP,GDHandle device,CWindowPtr window,
  24.     double LuminancePerVoltage,int frames,double darkLuminance);
  25.  
  26.  
  27. double GetALuminance(LuminanceRecord *LP,GDHandle device,
  28.     int frames,double LuminancePerVoltage,int entry,int red,int green,int blue)
  29. /*
  30. Get luminance produced by an RGB triplet. If frames==0 then ask for manual
  31. reading. If frames!=0 then use A/D to make automatic reading.
  32. */
  33. {
  34.     static double L;
  35.     static char string[100];
  36.     long finalTicks;
  37.     short left;
  38.  
  39.     LP->dacSize=Log2L(LP->VMax*2-1);    // in bits, rounded up
  40.     left=LP->leftShift=16-LP->dacSize;
  41.     LP->table[entry].rgb.red   = red<<left;
  42.     LP->table[entry].rgb.green = green<<left;
  43.     LP->table[entry].rgb.blue  = blue<<left;
  44.     LoadLuminances(device,LP,entry,entry);        /* load clut entry */
  45.     if(frames){
  46.         Delay(60L,&finalTicks);                    /* wait for photometer to settle */
  47.         LoadLuminances(device,LP,entry,entry);    /* synchronize to display */
  48.         L=VoltsDuringFrame(frames)*LuminancePerVoltage;
  49.     }
  50.     else{
  51.         printf("Please enter luminance in %s (%4.1f):",LP->units,L);
  52.         gets(string);
  53.         sscanf((char *)string,"%lf",&L);
  54.     }
  55.     return L;
  56. }
  57.  
  58.  
  59. void LToVHunt(LuminanceRecord *LP,GDHandle device,CWindowPtr window,
  60.     double LuminancePerVoltage,int frames,double darkLuminance)
  61. /*
  62. finds the DAC setting j, such that the desired luminance is
  63. between Lj and Lj+1. This is achieved by actually measuring
  64. the resulting luminances, before the LuminanceRecord is
  65. fully defined. The search proceeds by bisection:
  66. LP->LBackground==desired luminance;
  67. LP->VBackground==j;
  68. */
  69. {
  70.     int ju,jm,jl;
  71.     double LMeasured;
  72.     WindowPtr oldWindow;
  73.     int index=1;
  74.  
  75.     GetPort(&oldWindow);
  76.     SetPort((WindowPtr)window);
  77.     BringToFront((WindowPtr)window);
  78.     PmBackColor(index);
  79.     EraseRect(&window->portRect);
  80.     jl=0;
  81.     ju=LP->VMax+1;
  82.     while (ju-jl > 1) {
  83.         jm=(ju+jl)/2;
  84.         LMeasured=GetALuminance(LP,device,frames,LuminancePerVoltage
  85.             ,index,jm,jm,jm);
  86.         LMeasured-=darkLuminance;
  87.         if (LP->LBackground > LMeasured)
  88.             jl=jm;
  89.         else
  90.             ju=jm;
  91.     }
  92.     LP->VBackground=jl;
  93.     LP->table[index].rgb=(**(**(**device).gdPMap).pmTable).ctTable[index].rgb;
  94.     LoadLuminances(device,LP,index,index);        /* restore clut entry */
  95.     SendBehind((WindowPtr)window,NULL);
  96.     SetPort(oldWindow);
  97.     return;
  98. }
  99.